import { getCompanyInfoOverviewIntroduction } from '@/api/companyDetail/CompanyOverview'
import { Typography } from 'antd'
import { useEffect, useState } from 'react'

const AboutEnterprises = (props: any) => {
  const { companyCode } = props

  const [labelList, setLabelList] = useState<any[]>([])

  const [data, setdata]: any = useState({})

  const rows = 3

  const [expanded, setExpanded] = useState(false)

  const itemList: Array<any> = [
    {
      label: '法人：',
      key: 'legalRep',
      span: 273
    },
    {
      label: '注册资本：',
      key: 'regcapitalAmt',
      unit: '万人民币',
      span: 273
    },
    {
      label: '统一信用代码：',
      key: 'uniCode',
      span: 273
    },
    {
      label: '地址：',
      key: 'regAddress',
      span: '100%'
    },
    {
      label: '简介：',
      key: 'describer',
      span: '100%',
      showExport: true
    }
  ]

  function getInfo() {
    getCompanyInfoOverviewIntroduction({
      companyCode: companyCode as string
    }).then((res: any) => {
      setdata(res.data || {})
      setLabelList(res.data?.firstTag.concat(res.data.secondTag).filter((item: any) => item))
    })
  }
  function hsHandleNumDecimalPoint(value: any) {
    if (`${value}`.indexOf('.') > -1 && `${value}`.split('.')[1].length > 4) {
      value = value.toFixed(4)
    }
    value = Number(value)
    return value
  }
  const hsHandleMoneyShow = (value: number, unitFlag?: boolean) => {
    const emptyText = '-'
    let money = emptyText
    if (value) {
      // 单位万元
      let num: any = value
      // 单位元
      if (unitFlag) {
        num = value / 10000
      }
      if (num > 10000) {
        num = (num / 10000).toString()
        num = hsHandleNumDecimalPoint(Number(num)).toFixed(1)
        money = `${num}亿人民币`
      } else {
        num = hsHandleNumDecimalPoint(num).toFixed(0)
        money = `${num}万人民币`
      }
    }
    return money
  }

  //处理关于企业字段
  const itemData = (item: any) => {
    if (item === 'A级') item = 'A级纳税人'
    if (item === '其他') item = ''
    if (labelList.includes('上市企业')) {
      item = financingRound(item)
    }
    return item
  }

  //当“上市企业”标签出现时，不显示融资轮次相关标签
  const financingRound = (value: any) => {
    let data = ''
    const arr = ['B轮', 'C轮', '天使轮', '种子轮', 'A轮', 'D轮', 'E轮及以上', 'IPO']
    data = arr.includes(value) ? '' : value
    return data
  }

  useEffect(() => {
    getInfo()
  }, [])

  return (
    <div className="about-enterprises" id="关于企业">
      {labelList && labelList.length ? (
        <div className="label-box">
          {labelList.map((item: any, index: number) => (
            <span key={index} className="label">
              {itemData(item)}
            </span>
          ))}
        </div>
      ) : null}
      <div className="flex items-baseline flex-wrap">
        {itemList.map((item: any, index: number) => (
          <div className="about-item" key={index} style={{ width: item.span }}>
            <div className="item-label">{item.label}</div>
            <div className="item-value">
              {item.label === '注册资本：' ? (
                <div>{hsHandleMoneyShow(data[item.key]) || '-'}</div>
              ) : (
                <Typography.Paragraph
                  ellipsis={{
                    rows,
                    expandable: 'collapsible',
                    expanded,
                    onExpand: (_, _info) => setExpanded((c) => !c)
                  }}>
                  {data[item.key] || '-'}
                </Typography.Paragraph>
              )}
            </div>
          </div>
        ))}
      </div>
    </div>
  )
}
export default AboutEnterprises
